<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>费用管理系统 - 登录</title>
    <script src="https://cdn.tailwindcss.com"></script>
    <style>
        .login-bg {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
    </style>
</head>
<body class="login-bg min-h-screen flex items-center justify-center">
    <div class="bg-white p-8 rounded-2xl shadow-2xl w-full max-w-md">
        <div class="text-center mb-8">
            <h1 class="text-3xl font-bold text-gray-800 mb-2">费用管理系统</h1>
            <p class="text-gray-600">管理员登录</p>
        </div>

        <form id="loginForm" class="space-y-6">
            <div>
                <label for="username" class="block text-sm font-medium text-gray-700 mb-2">用户名</label>
                <input type="text" id="username" name="username" required
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none transition-all">
            </div>

            <div>
                <label for="password" class="block text-sm font-medium text-gray-700 mb-2">密码</label>
                <input type="password" id="password" name="password" required
                    class="w-full px-4 py-3 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-transparent outline-none transition-all">
            </div>

            <button type="submit" id="loginBtn"
                class="w-full bg-blue-600 text-white py-3 rounded-lg hover:bg-blue-700 focus:ring-2 focus:ring-blue-500 focus:ring-offset-2 transition-all font-medium">
                登录
            </button>
        </form>

        <div id="message" class="mt-4 text-center text-sm hidden"></div>

        <div class="mt-8 text-center text-sm text-gray-500">
            <p>默认账号: admin / 123456</p>
        </div>
    </div>

    <script>
        const API_BASE = '/api';
        
        // 检查是否已登录
        if (localStorage.getItem('token')) {
            window.location.href = '/admin/dashboard.html';
        }

        document.getElementById('loginForm').addEventListener('submit', async (e) => {
            e.preventDefault();
            
            const username = document.getElementById('username').value;
            const password = document.getElementById('password').value;
            const loginBtn = document.getElementById('loginBtn');
            const message = document.getElementById('message');

            loginBtn.textContent = '登录中...';
            loginBtn.disabled = true;

            try {
                const response = await fetch(`${API_BASE}/auth/login`, {
                    method: 'POST',
                    headers: {
                        'Content-Type': 'application/json'
                    },
                    body: JSON.stringify({ username, password })
                });

                const data = await response.json();

                if (response.ok) {
                    localStorage.setItem('token', data.token);
                    localStorage.setItem('user', JSON.stringify(data.user));
                    
                    message.textContent = '登录成功，正在跳转...';
                    message.className = 'mt-4 text-center text-sm text-green-600';
                    message.classList.remove('hidden');

                    setTimeout(() => {
                        window.location.href = '/admin/dashboard.html';
                    }, 1000);
                } else {
                    message.textContent = data.message || '登录失败';
                    message.className = 'mt-4 text-center text-sm text-red-600';
                    message.classList.remove('hidden');
                }
            } catch (error) {
                message.textContent = '网络错误，请稍后重试';
                message.className = 'mt-4 text-center text-sm text-red-600';
                message.classList.remove('hidden');
            }

            loginBtn.textContent = '登录';
            loginBtn.disabled = false;
        });
    </script>
</body>
</html>